package body Legend.Controls is

   procedure Paint_Controls (
      List   : in out Control_Lists.List;
      Target : in out Renders.Canvas_Type'Class
      )
   is
      now : Time := Clock;
      i : Control_Lists.Cursor := List.First;
      j : Control_Lists.Cursor;
      c : Control_Access;
   begin
      while Control_Lists.Has_Element(i) loop
         j := Control_Lists.Next(i);
         c := Control_Lists.Element(i);
         c.Prepare;
         if c.Visible then
            Target.Draw_Canvas(c.Canvas, c.Position);
         end if;
         if now > c.Life then
            if c.Managed then
               List.Delete(i);
               Delete(c);
            else
               c.Visible := False;
            end if;
         end if;
         i := j;
      end loop;
   end Paint_Controls;

   function Compare_By_Z_Index (C1, C2 : Control_Access) return Boolean
   is
   begin
      return C1.Z_Index > C2.Z_Index;
   end Compare_By_Z_Index;

   procedure Autosize (this : in out Label_Type; Value : Boolean)
   is
   begin
      this.F_Autosize := Value;
   end Autosize;

   function Text (this : Label_Type) return UTF8_String
   is
   begin
      return Unbounded.To_String(this.F_Text);
   end Text;

   procedure Text (this : in out Label_Type; Value : UTF8_String)
   is
   begin
      Unbounded.Set_Unbounded_String(this.F_Text, Value);
   end Text;

   function Font (this : in out Label_Type) return access Font_Type 
   is
   begin
      return this.F_Font'Unchecked_Access;
   end Font;

   procedure Font (this : in out Label_Type; Value : Font_Type)
   is
   begin
      this.F_Font := Value;
   end Font;

   overriding
   procedure Paint (this : in out Label_Type)
   is
      use Interfaces.C;
      em : constant double := double(this.Font.Size);

      R : SDL.Video.Rect_t := (0, 0, 0, 0);
      brush : Cairo.Context.Cairo_Context_Ref;
      tx : double := 0.0;
      ty : double := em;
   begin
      if this.F_Autosize then
         R.W := Uint16_t(UTF8_Length(this.Text)) * this.Font.Size;
         R.H := this.Font.Size + 2;
         this.Size.x := Int16_t(R.W);
         this.Size.y := Int16_t(R.H);
      else
         R.W := Uint16_t(this.Size.x);
         R.H := Uint16_t(this.Size.y);
      end if;
      this.Canvas.Bind(Region => R);
      this.Canvas.Clear(this.Back_Color);
      brush := this.Canvas.As_Brush;
      this.Canvas.Set_Brush_Color(this.Font.Color);
      this.Canvas.Set_True_Font(Unbounded.To_String(this.Font.Name));
      brush.Set_Font_Size(em);
      brush.Move_To(tx, ty);
      brush.Show_Text(this.Text);
      brush.Stroke;
      this.Canvas.Accept_Brush_Changed;
   end Paint;

   procedure Initialize (this : in out Label_Type)
   is
   begin
      Control_Type(this).Initialize;
      this.F_Font.Color := Colors.Black;
      this.F_Font.Size := 16;
      this.F_Font.Name := Unbounded.To_Unbounded_String("Default");
      this.F_Autosize := True;
   end Initialize;

   procedure Paint (this : in out Fog_Type)
   is
   begin
      this.Canvas.Bind;
      this.Canvas.Clear(this.Back_Color);
   end Paint;

   procedure Initialize (this : in out Fog_Type)
   is
   begin
      Control_Type(this).Initialize;
      this.Back_Color.A := 128;
   end Initialize;

end Legend.Controls;
